package com.ejlchina.tool.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.adolf.androiddemo.databinding.ItemFunctionBinding
import com.ejlchina.tool.bean.FunctionBean
import com.ejlchina.tool.util.showToast
import com.huyuhui.fastble.BleManager

class DebugPageFunctionAdapter(private val dataList: MutableList<FunctionBean>, private val itemClick:(FunctionBean) -> Unit): RecyclerView.Adapter<DebugPageFunctionViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DebugPageFunctionViewHolder {
        val binding = ItemFunctionBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return DebugPageFunctionViewHolder(binding, itemClick)
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    override fun onBindViewHolder(holder: DebugPageFunctionViewHolder, position: Int) {
        holder.bindData(dataList[position])
    }
}

class DebugPageFunctionViewHolder(private val binding: ItemFunctionBinding, private val itemClick:(FunctionBean) -> Unit): RecyclerView.ViewHolder(binding.root){
    fun bindData(data: FunctionBean){
        binding.ivImage.setImageResource(data.imgResId)
        binding.tvText.setText(data.textResId)
        binding.root.setOnClickListener {
            if (BleManager.getAllConnectedDevice().isEmpty()) {
                showToast("还未连接设备")
                return@setOnClickListener
            }
            itemClick(data)
        }
    }
}